/**
 * 
 */
package com.gmail.hy87cn.hellohi.client;

import com.gmail.hy87cn.hellohi.shared.ERoomState;
import com.gmail.hy87cn.hellohi.shared.HiMessage;
import com.gmail.hy87cn.hellohi.shared.HiRoom;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author HeYi
 * 
 */
public class HiChatUi extends Composite {

	private HiChatClient client;
	private HiTitle title = new HiTitle();

	public void setClient(HiChatClient client) {
		this.client = client;
	}

	private static HiChatUiUiBinder uiBinder = GWT
			.create(HiChatUiUiBinder.class);

	interface HiChatUiUiBinder extends UiBinder<Widget, HiChatUi> {
	}

	@UiField
	ScrollPanel scrollPanel;
	@UiField
	FlowPanel contentPanel;
	@UiField
	Button stateButton;
	@UiField
	RichTextArea inputText;

	public HiChatUi() {
		initWidget(uiBinder.createAndBindUi(this));
		onRoomUpdated(null);
	}

	private void sendMessage() {
		String input = inputText.getHTML();
		if (!input.trim().isEmpty() && 
				client.getRoom() != null && client.getRoom().getState() == ERoomState.Locked) {
			client.sendMessage(input);
			inputText.setHTML(null);
			contentPanel.add(new HiMessageUi("You", input));
			scrollPanel.scrollToBottom();
		}
	}

	@Override
	protected void onUnload() {
		client.releaseToken();
		super.onUnload();
	}

	@UiHandler("stateButton")
	void onClick(ClickEvent e) {
		client.switchState();
	}

	@UiHandler("inputText")
	void onKeyUp(KeyUpEvent event) {
		if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
			inputText.setText(inputText.getText().trim());
			sendMessage();
		}
	}

	public void onMessageUpdated(HiMessage[] messages) {
		if (messages.length > 0) {
			for (HiMessage message : messages) {
				contentPanel.add(new HiMessageUi("Stranger", message.getContent()));
			}
			title.setTitle(messages[0].getContent());
			scrollPanel.scrollToBottom();
		}
	}

	public void onMessageSendFailed(String message) {
		contentPanel.add(new HiMessageUi("Send failed", message));
		scrollPanel.scrollToBottom();
	}

	public void onRoomUpdated(HiRoom room) {
		if (room == null) {
			stateButton.setEnabled(true);
			stateButton.setText("Connect");
			inputText.setEnabled(false);
			inputText.setText(null);
		} else {
			switch (room.getState()) {
			case Open:
				stateButton.setEnabled(false);
				stateButton.setText("Waiting...");
				inputText.setEnabled(false);
				inputText.setText(null);
				contentPanel.clear();
				break;
			case Locked:
				stateButton.setEnabled(true);
				stateButton.setText("Disconnect");
				inputText.setEnabled(true);
				break;
			}

			contentPanel.clear();
			for (HiMessage message : room.getMessageList()) {
				if (message.getUserId() != client.getToken()) {
					contentPanel.add(new HiMessageUi("Stranger", message.getContent()));
				} else {
					contentPanel.add(new HiMessageUi("You", message.getContent()));
				}
			}
			scrollPanel.scrollToBottom();
		}
	}
}
